package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.XMLData;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.data.json.JsonSendData;
import com.nexgo.data.json.JsonTools;
import com.nexgo.data.json.bean.JsonBody;
import com.nexgo.data.json.bean.SendBodyData;
import com.nexgo.data.json.bean.SendHead;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.TransRecord;
import com.nexgo.payment.entity.MessageData;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.XmlUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 扫码撤销组包
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年9月14日
 */
class ScanVoidBuilder extends DataBuilder{

    private final TransRecord mTransRecord;

    ScanVoidBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
        mTransRecord = mRepository.queryScanTransRecord(mTransData.getTraceCode());
    }

    @Override
    public SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setMsgType(mTransData.getTransType().getMsgType());

        sendData.setProcCode(mTransData.getTransType().getProcCode());
        sendData.setAmount(StringUtils.leftPad(mTransRecord.getAmount(), 12, '0'));
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        setCurruntTraceNumber(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        mRepository.increaseTraceNumber();

        sendData.setTerminalId(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        sendData.setMerchantId(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        sendData.setAdditionalData(formField48());
        sendData.setFundType(mTransData.getFundType());

        sendData.setField60(formField60());
        sendData.setField62(formField62());
        sendData.setMac("0000000000000000");
        return sendData;
    }

    @Override
    public MessageData formJsonData() {
        MessageData sSendData = new MessageData();
        JsonSendData jsonSendData = new JsonSendData();
        SendHead head = new SendHead();
        JsonBody jsonBody = new JsonBody();

        //head
        head.setTRAN_PROCESS("");
        head.setTRAN_ID("");
        head.setTranscode("CIPP010201");
        String date = new SimpleDateFormat("yyyyMMddHHmmss", SysConstant.LOCALE).format(new Date());
        head.setTerm_trans_time(date);
        head.setMcht_id(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        //经纬度，签名暂时为空
        head.setLngx(mRepository.getParamValue(PreferencesConstants.LONGITUDE));
        head.setLngy(mRepository.getParamValue(PreferencesConstants.LATITUDE));
        head.setSign("0000000000000000");
        jsonSendData.setREQ_HEAD(head);


        SendData sendData  = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sSendData.setSendData(sendData);

        //body
        jsonBody.setTerm_id(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        jsonBody.setTerm_batch_no(mTransData.getOrgBatchNum());
        jsonBody.setTerm_pos_no(mTransData.getOrgTraceNum());
        jsonBody.setSys_order_no(mTransData.getTraceCode());
        jsonBody.setRefund_amount(mTransData.getAmount());
        jsonBody.setRefund_amount_type("156");

        SendBodyData sendBodyData = new SendBodyData();
        String str = JsonTools.toJson(jsonBody);
        sendBodyData.setDECRYPT(str);

        jsonSendData.setREQ_BODY(sendBodyData);

        sSendData.setJsonSendData(jsonSendData);

        setScan(true);

        return sSendData;
    }

    private String formField48() {
        StringBuilder sb = new StringBuilder();

        Map<String, String> xmlMap = new HashMap<>();
        XMLData xmlData = new XMLData();

        //head
        xmlData.setTranscode("CIPP010101");
        String date = new SimpleDateFormat("yyyyMMddHHmmss", SysConstant.LOCALE).format(new Date());
        xmlData.setTerm_trans_time(date);
        xmlData.setMcht_id(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        //body
        xmlData.setTerm_id(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        xmlData.setTerm_batch_no(mTransData.getOrgBatchNum());
        xmlData.setTerm_pos_no(mTransData.getOrgTraceNum());
        xmlData.setSys_order_no(mTransData.getTraceCode());
        xmlData.setRefund_amount(mTransRecord.getAmount());
        xmlData.setAmount_type("156");
        sb.append(XmlUtils.formXML(xmlData));

        return sb.toString();
    }

    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(SysConstant.LOCALE, "%02d",
                Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "1"))));
        sb.append("0000");
        sb.append("6");
        sb.append(mTransData.isFallback() ? "2" : "0");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField62() {
        StringBuilder sb = new StringBuilder();
        sb.append(mTransData.getTransType().getFuncCode());
        sb.append(getCurruntTraceNumber());
//        sb.append("000");
//        sb.append("6");
//        sb.append(mTransData.isFallback() ? "2" : "0");
        return sb.toString();
    }
}
